/**
 * 
 */
package com.haizhi.user.controller;

import java.beans.IntrospectionException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haizhi.bean.QTbUser;
import com.haizhi.bean.TbAdmin;
import com.haizhi.bean.TbFeed;
import com.haizhi.bean.TbUser;
import com.haizhi.bean.TbUserActivity;
import com.haizhi.bean.TbUserFollow;
import com.haizhi.bean.TbUserMessage;
import com.haizhi.service.FeedService;
import com.haizhi.service.UserActivityService;
import com.haizhi.service.UserFollowService;
import com.haizhi.service.UserMessageService;
import com.haizhi.service.UserService;
import com.haizhi.user.vo.ActivityVo;
import com.haizhi.user.vo.ClientUserVo;
import com.haizhi.user.vo.MessageVo;
import com.haizhi.user.vo.UserMessageVo;
import com.haizhi.util.BaseVoUtil;
import com.haizhi.util.MyAES;
import com.haizhi.util.MyMap;
import com.haizhi.util.MyMapBuilder;
import com.haizhi.util.MyMapBuilder.ErrorCode;
import com.haizhi.util.ResponseEntityUtil;


import com.querydsl.core.types.Predicate;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author jiangjintai
 *
 */
@Controller
@RequestMapping("/user")
@Api(description="用户版块", value = "用户版块")//添加注释  
public class UserController {
	public static final String token_password="jjt1024";
	public static final Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	UserMessageService userMessageService;
	@Autowired
	FeedService feedService;
	@Autowired
	UserFollowService userFollowService;
	@Autowired
	UserActivityService userActivityService;
	@Autowired 
	UserService userService;
	@RequestMapping("/register")
	@ResponseBody
	@ApiOperation(value = "用户注册",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> register(
			TbUser tbUser,
			@ApiParam(value="用户账户",name="userAccount",required=true)@RequestParam(required=true)String userAccount,
			@ApiParam(value="用户名",name="userName",required=true)@RequestParam(required=true)String userName,
			@ApiParam(value="用户密码",name="userPassword",required=true)@RequestParam(required=true)String userPassword,
			@ApiParam(value="用户手机",name="userPhone",required=true)@RequestParam(required=true)String userPhone,
			@ApiParam(value="用户性别",name="userSex",required=true)@RequestParam(required=true)String userSex,
			@ApiParam(value="用户细节",name="userDetail",required=true)@RequestParam(required=true)String userDetail,
			@ApiParam(value="用户密保问题",name="userPasswordQuestion",required=true)@RequestParam(required=true)String userPasswordQuestion,
			@ApiParam(value="用户密保答案",name="userPasswordAnswer",required=true)@RequestParam(required=true)String userPasswordAnswer
			){
		if(tbUser.getUserAccount().trim().equals("")||tbUser.getUserName().trim().equals("")||tbUser.getUserPassword().trim().equals("")||tbUser.getUserPhone().trim().isEmpty())
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "帐号，密码，昵称,手机不能为空"),HttpStatus.BAD_REQUEST);
		if(userService.findByUserAccount(userAccount)!=null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.loginFailError, "帐号被使用"),HttpStatus.UNAUTHORIZED);
		if(userService.findByUserPhone(userPhone)!=null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.phoneUsedError, "手机被使用"),HttpStatus.UNAUTHORIZED);
		if(userService.findByUserName(userName)!=null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.nameUsedError, "昵称被使用"),HttpStatus.UNAUTHORIZED);
		tbUser.setUserCollectionCount(0);
		tbUser.setUserFansCount(0);
		tbUser.setUserFollowerCount(0);
		tbUser.setUserRegisterTime(new Date());
		tbUser.setUserStatus(1);
		userService.save(tbUser);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("注册成功"),HttpStatus.CREATED);
	}
	
	@RequestMapping("/login")
	@ResponseBody
	@ApiOperation(value = "用户登录",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> login(
			@ApiParam(value="用户账户",name="userAccount",required=true)@RequestParam(required=true)String userAccount,
			@ApiParam(value="用户密码",name="userPassword",required=true)@RequestParam(required=true)String userPassword
			) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		if(userAccount.trim().isEmpty()||userPassword.trim().isEmpty())
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "帐号或密码不能为空"),HttpStatus.BAD_REQUEST);
		
		//使用querydsl
		QTbUser qtbUser = QTbUser.tbUser;
		Predicate p = qtbUser.userAccount.eq(userAccount).and(qtbUser.userPassword.eq(userPassword)).and(qtbUser.userStatus.eq(1));
		TbUser tbUser = userService.findByUserAccountAndUserPasswordUseQuerydsl(p);
		
//		TbUser tbUser = userService.findByUserAccountAndUserPassword(userAccount,userPassword);
		if(tbUser==null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.loginFailError, "帐号或者密码错误"),HttpStatus.UNAUTHORIZED);
		else{
			//登录成功
			//生成token
			String token = MyAES.encrypt(tbUser.getUserId().toString(), UserController.token_password);
			tbUser.setUserCurrentToken(token);
			//设置用户登录时间
			tbUser.setUserLastLoginTime(tbUser.getUserLoginTime());
			tbUser.setUserLoginTime(new Date());
			userService.save(tbUser);
			//返回user
			UserMessageVo vo = BaseVoUtil.getVo(tbUser, new UserMessageVo());
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap(MyMapBuilder.buildMap().put("user", vo)),HttpStatus.OK);
		}
	}
	
	@RequestMapping("/getPasswordQuestion")
	@ResponseBody
	@ApiOperation(value = "获取密保问题",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getPasswordQuestion(
			@ApiParam(value="用户账户",name="userAccount",required=true)@RequestParam(name="userAccount",required=true)String userAccount
			){
		if(userAccount.trim().isEmpty())
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "帐号不能为空"),HttpStatus.BAD_REQUEST);
		TbUser tbUser = userService.findByUserAccount(userAccount);
		if(tbUser==null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.loginFailError, "帐号错误"),HttpStatus.UNAUTHORIZED);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap(MyMapBuilder.buildMap().put("userPasswordQuestion", tbUser.getUserPasswordQuestion())),HttpStatus.OK);
	}
	
	@RequestMapping("/confirmPasswordQuestion")
	@ResponseBody
	@ApiOperation(value = "确认密保答案",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> confirmPasswordQuestion(
			@ApiParam(value="用户账户",name="userAccount",required=true)@RequestParam(name="userAccount",required=true)String userAccount,
			@ApiParam(value="用户答案",name="userAnswer",required=true)@RequestParam(name="userAnswer",required=true)String userAnswer
			) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException{
		if(userAccount.trim().isEmpty()||userAnswer.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.loginFailError, "帐号或答案不能为空");
		TbUser tbUser = userService.findByUserAccount(userAccount);
		if(tbUser==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.loginFailError, "帐号不存在");
		if(!tbUser.getUserPasswordAnswer().equals(userAnswer))
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "答案错误");
		
		String token = MyAES.encrypt(tbUser.getUserPassword().toString(), token_password);
		tbUser.setUserCurrentToken(token);
		userService.save(tbUser);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildMap().put("token", token),HttpStatus.OK);
		
	}

	@RequestMapping("/findPassword")
	@ResponseBody
	@ApiOperation(value = "修改密码",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> findPassword(
			@ApiParam(value="用户token",name="token",required=true)@RequestParam(name="token",required=true)String token,
			@ApiParam(value="用户账户",name="userAccount",required=true)@RequestParam(name="userAccount",required=true)String userAccount,
			@ApiParam(value="用户新密码",name="newPassword",required=true)@RequestParam(name="newPassword",required=true)String newPassword
			){
		if(token.trim().isEmpty()||userAccount.trim().isEmpty()||newPassword.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.loginFailError, "帐号，token，密码不能为空");
		TbUser tbUser = userService.findByUserAccount(userAccount);
		if(!tbUser.getUserCurrentToken().equals(token))
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.loginFailError, "token错误");
		tbUser.setUserPassword(newPassword);
		userService.save(tbUser);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap(MyMapBuilder.buildMap().put("token", token)),HttpStatus.OK);
	}
	/************************************需要拦截*************************************************/
	@RequestMapping("/updatePasswordQuestion")
	@ResponseBody
	@ApiOperation(value = "修改密保问题",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getPasswordQuestion(
			@ApiParam(value="用户token",name="token",required=true)@RequestParam(name="token",required=true)String token,
			@ApiParam(value="用户账户",name="userAccount",required=true)@RequestParam(name="userAccount",required=true)String userAccount,
			@ApiParam(value="用户新密保问题",name="newQuestion",required=true)@RequestParam(name="newQuestion",required=true)String newQuestion,
			@ApiParam(value="用户新密保答案",name="newAnswer",required=true)@RequestParam(name="newAnswer",required=true)String newAnswer
			){
		if(token.trim().isEmpty()||userAccount.trim().isEmpty()||newQuestion.trim().isEmpty()||newAnswer.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "token,账户,问题，问题答案不能为空");
		TbUser tbUser= userService.findByUserAccount(userAccount);
		if(tbUser==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "该帐号不存在");
		if(!tbUser.getUserCurrentToken().equals(token))
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "token错误");
		tbUser.setUserPasswordQuestion(newQuestion);
		tbUser.setUserPasswordAnswer(newAnswer);
		userService.save(tbUser);
		return new  ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap(MyMapBuilder.buildMap().put("userPassword", newQuestion)),HttpStatus.OK);
	}
	

	@RequestMapping("/changePassword")
	@ResponseBody
	@ApiOperation(value = "修改密码",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> changePassword (
			@ApiParam(value="旧密码",name="oldPassword",required=true)@RequestParam(name="oldPassword",required=true)String oldPassword,
			@ApiParam(value="新密码",name="newPassword",required=true)@RequestParam(name="newPassword",required=true)String newPassword,
			HttpServletRequest request
			){
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		if(oldPassword.trim().isEmpty()||newPassword.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "密码不能为空");
		if(!tbUser.getUserPassword().equals(oldPassword)){
			logger.info(oldPassword);
			logger.info(tbUser.getUserPassword());
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "密码错误");
		}
			
		tbUser.setUserPassword(newPassword);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("修改成功"),HttpStatus.OK);
	}
	
	@RequestMapping("/startUser")
	@ResponseBody
	@ApiOperation(value = "关注某个用户",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> startUser(
			@ApiParam(value="目标用户id",name="targetUserId",required=true)@RequestParam(name="targetUserId",required=true)String targetUserId,
			@ApiParam(value="type1表示关注，0表示取消关注",name="type",required=true)@RequestParam(name="type",required=true)String type,
			HttpServletRequest request
			){

		TbUser tbUserRequest = (TbUser)request.getAttribute("tbUser");
		if(tbUserRequest.getUserId().equals(targetUserId))
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "用户不能自己关注自己"),HttpStatus.NOT_FOUND);
		if(type.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "type类型错误");
		TbUser tbUser = userService.findById(Integer.valueOf(tbUserRequest.getUserId()));
		TbUser targetTbUser = userService.findById(Integer.valueOf(targetUserId));
		if(tbUser==null||targetTbUser==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "用户id不存在");
		if(type.equals("1")){
			//关注
			//判断是否重复关注
			TbUserFollow tbUserFollow = null;
			tbUserFollow = userFollowService.findByTbUserByFollowUserIdAndTbUserByUserId(targetTbUser,tbUser);
			if(tbUserFollow!=null)
				return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "不可重复关注");
			tbUserFollow = new TbUserFollow();
			tbUserFollow.setTbUserByUserId(tbUser);
			tbUserFollow.setTbUserByFollowUserId(targetTbUser);
			tbUserFollow.setFollowTime(new Date());
			userFollowService.save(tbUserFollow);
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("操作成功"),HttpStatus.OK);
		}else if(type.equals("0")){
			TbUserFollow tbUserFollow =  userFollowService.findByUserIdAndFollowerUserId(tbUser.getUserId(),targetTbUser.getUserId());
			if(tbUserFollow!=null)
				userFollowService.delete(tbUserFollow);
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("操作成功"),HttpStatus.OK);
		}else{
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "type类型错误");
		}
			
		
		
	}
	
	@RequestMapping("/addActivity")
	@ResponseBody
	@ApiOperation(value = "发表动态",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> addActivity(
			@ApiParam(value="内容",name="content",required=true)@RequestParam(name="content",required=true)String content,
			@ApiParam(value="type表示动态 默认为1",name="type",required=true)@RequestParam(name="type",defaultValue="1",required=true)String type,
			HttpServletRequest request
			){
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		if(!type.equals("1"))
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "type类型错误");
		userService.saveActivityAndAlloc(tbUser,content);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功"),HttpStatus.OK);
	
	}
	
	@RequestMapping("/getUserActivity")
	@ResponseBody
	@ApiOperation(value = "获取用户动态",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getUserActivity(
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble,
			HttpServletRequest request,
			@ApiParam(value="用户id,填写时获取具体用户动态，不填时获取自己",name="userId",required=true)@RequestParam(name="userId",required=true)String userId		
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUserLogin = (TbUser) request.getAttribute("tbUser");
		TbUser tbUser = null;
		if(userId==null||userId.trim().isEmpty()){
			tbUser = tbUserLogin;
		}else{
			tbUser = userService.findById(Integer.valueOf(userId));
			if(tbUser==null)
				return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "该用户id不存在");
		}
		Page<TbUserActivity> tbUserActivityPage = userActivityService.findByUserId(tbUser.getUserId(),pageAble);
		List<TbUserActivity> tbActivityList=tbUserActivityPage.getContent();
		List<ActivityVo> activityVoList = new ArrayList<ActivityVo>();
		
		for(TbUserActivity tua : tbActivityList){
			ActivityVo activityVo = BaseVoUtil.getVo(tua, new ActivityVo());
			activityVo.setUserId(tua.getTbUser().getUserId());
			activityVo.setUserName(tua.getTbUser().getUserName());//添加用户名
			activityVoList.add(activityVo);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildMap().put("state", true).put("totalPage", tbUserActivityPage.getTotalPages()).put("nowPage", tbUserActivityPage.getNumber()).put("data", activityVoList),HttpStatus.OK);
		
	}
	
	@RequestMapping("/getUserActivityList")
	@ResponseBody
	@ApiOperation(value = "获取用户关注的用户的动态列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getUserActivityList(
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble,
			HttpServletRequest request
			){
		
		TbUser tbUserLogin = (TbUser) request.getAttribute("tbUser");
		TbUser tbUser = null ; 
		tbUser = tbUserLogin;
		Page<TbFeed> feedPage = feedService.findByTbUserByUserIdUserIdOrderByFeedIdDesc(tbUser.getUserId(),pageAble);
		List<ActivityVo> activityVoList = new ArrayList<ActivityVo>();
		List<TbFeed> feedList = feedPage.getContent();
		for(TbFeed tbFeed:feedList){
			ActivityVo activityVo = new ActivityVo();
			activityVo.setActivityContent(tbFeed.getFeedMessage());
			activityVo.setActivityTime(tbFeed.getFeedAddTime());
			activityVo.setActivityType(1);
			activityVo.setUserId(tbFeed.getTbUserByTargetId().getUserId());
			activityVo.setUserName(tbFeed.getTbUserByTargetId().getUserName());
			activityVoList.add(activityVo);
		}
		
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildMap().put("state", true).put("totalPage", feedPage.getTotalPages()).put("nowPage", feedPage.getNumber()).put("data", "获取成功").put("activity", activityVoList),HttpStatus.OK);
	}
	
	@RequestMapping("/getUserFollowersList")
	@ResponseBody
	@ApiOperation(value = "获取用户粉丝列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getUserFollowersList(
			@ApiParam(value="用户id",name="userId",required=false)@RequestParam(name="userId")String userId,
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser user = (TbUser) request.getAttribute("tbUser");
		Page<TbUserFollow> tbUserFollowPage=null;
		if(userId==null||userId.trim().isEmpty()){
			//代表寻找自己的粉丝
			tbUserFollowPage = userFollowService.findByFollowUserId(user.getUserId(),pageAble);
		}else{
			tbUserFollowPage = userFollowService.findByFollowUserId(Integer.valueOf(userId),pageAble);
		}
		List<ClientUserVo> userMessageVo = new ArrayList<ClientUserVo>();
		List<TbUserFollow> userFollowList =  tbUserFollowPage.getContent();
		for(TbUserFollow tbUserFollow:userFollowList){
			TbUser tbUserFans = userService.findById(tbUserFollow.getTbUserByUserId().getUserId());
			ClientUserVo cuv =  BaseVoUtil.getVo(tbUserFans, new ClientUserVo());
			//判断关系
			this.setClientUserRelation(tbUserFans, cuv, user);
			
			userMessageVo.add(cuv);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildMap().put("state", true).put("totalPage", tbUserFollowPage.getTotalPages()).put("nowPage", tbUserFollowPage.getNumber()).put("data", "获取成功").put("user", userMessageVo),HttpStatus.OK);
	}
	
	@RequestMapping("/getUserMessageList")
	@ResponseBody
	@ApiOperation(value = "获取用户消息列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getUserMessageList(
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		Page<TbUserMessage> tbUserMessagePage = userMessageService.findByReceiveUserId(tbUser.getUserId(),pageAble);
		List<TbUserMessage> tbUserMessageList  = tbUserMessagePage.getContent();
		List<MessageVo> messageVoList = new ArrayList<MessageVo>(); 
		for(TbUserMessage tbUserMessage:tbUserMessageList){
			MessageVo messageVo = BaseVoUtil.getVo(tbUserMessage, new MessageVo());
			messageVo.setSendUserId(tbUserMessage.getTbUserByPostUserId().getUserId());
			messageVo.setSendUserName(tbUserMessage.getTbUserByPostUserId().getUserName());
			messageVo.setSendUserSex(tbUserMessage.getTbUserByPostUserId().getUserSex());
			messageVoList.add(messageVo);
		}
		
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildMap().put("state", true).put("totalPage", tbUserMessagePage.getTotalPages()).put("nowPage", tbUserMessagePage.getNumber()).put("data", messageVoList),HttpStatus.OK);
	}
	
	@ResponseBody
	@RequestMapping("/sendMessage")
	@ApiOperation(value = "发送信息",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>>sendMessage(
			@ApiParam(value="用户id",name="userId",required=true)@RequestParam(name="userId",required=true)String userId,
			@ApiParam(value="用户发送消息",name="message",required=true)@RequestParam(name="message",required=true)String message,
			HttpServletRequest request
			){
		
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		TbUser targerTbUser = userService.findById(Integer.valueOf(userId));
		if(targerTbUser==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "该用户id不在");
		userMessageService.saveSendMessage(tbUser,targerTbUser,message);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("发送成功"),HttpStatus.OK);	
	}
	
	@RequestMapping("/getUserStartList")
	@ApiOperation(value = "获取用户关注列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getUserStartList(
			@ApiParam(value="用户id",name="userId",required=false)@RequestParam(name="userId")String userId,
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		Page<TbUserFollow> tbUserFollowPage =null;
		TbUser tbUser=null;
		if(userId==null||userId.trim().isEmpty()){
			tbUser = (TbUser) request.getAttribute("tbUser");
			tbUserFollowPage = userFollowService.findByTbUserByUserId(tbUser,pageAble);
		}else{
			tbUser = userService.findById(Integer.valueOf(userId));
			if(tbUser==null)
				return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "userId不存在");
			tbUserFollowPage = userFollowService.findByTbUserByUserId(tbUser,pageAble);
		}
		
		List<ClientUserVo> userVoList = new ArrayList<ClientUserVo>();
		List<TbUserFollow> userFollowList= tbUserFollowPage.getContent();
		for(TbUserFollow  t : userFollowList){
			TbUser tbUserTarget = userService.findById(t.getTbUserByFollowUserId().getUserId());
			ClientUserVo clientUserVo =BaseVoUtil.getVo(tbUserTarget, new ClientUserVo());
			//判断用户之间关系
			TbUser tbUserLogin = (TbUser) request.getAttribute("tbUser");
			setClientUserRelation(tbUserTarget, clientUserVo, tbUserLogin);
			userVoList.add(clientUserVo);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("user",userVoList).put("nowPage", tbUserFollowPage.getNumber()).put("totalPage", tbUserFollowPage.getTotalPages()),HttpStatus.OK);
		
	}

	/**
	 * jiangjintai
	 * 2016年8月17日
	 * @param t
	 * @param clientUserVo
	 * @param tbUserLogin
	 */
	private void setClientUserRelation(TbUser t, ClientUserVo clientUserVo,
			TbUser tbUserLogin) {
		System.out.println("目标对象用户id："+t.getUserId());
		System.out.println("登录对象用户id："+tbUserLogin.getUserId());
		TbUserFollow tbUserFollowIsFans = userFollowService.findByTbUserByFollowUserIdAndTbUserByUserId(tbUserLogin,t);
		System.out.println(tbUserFollowIsFans);
		TbUserFollow tbUserFollowIsStar = userFollowService.findByTbUserByFollowUserIdAndTbUserByUserId(t,tbUserLogin);
		System.out.println(tbUserFollowIsStar);
		if(tbUserFollowIsFans!=null&&tbUserFollowIsStar!=null){//相互关注
			clientUserVo.setRelationName("相互关注");
			clientUserVo.setRelationStatus(2);
		}else if(tbUserFollowIsFans==null&&tbUserFollowIsStar!=null){//关注了他
			clientUserVo.setRelationName("关注了他");
			clientUserVo.setRelationStatus(1);
		}else if(tbUserFollowIsFans!=null&&tbUserFollowIsStar==null){//关注了我
			clientUserVo.setRelationName("关注了我");
			clientUserVo.setRelationStatus(-1);
		}else{//没有互相关注
			clientUserVo.setRelationName("陌生人");
			clientUserVo.setRelationStatus(0);
		}
	}
	
	@RequestMapping("/getOtherUserDetail")
	@ApiOperation(value = "用户信息",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getOtherUserDetail(
			@ApiParam(value="用户id",name="userId",required=true)@RequestParam("userId")String userId,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUserLogin = (TbUser) request.getAttribute("tbUser");
		TbUser tbUser = userService.findById(Integer.valueOf(userId));
		if(tbUser==null)
			return new ResponseEntity<>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "该用户id不存在"),HttpStatus.NOT_FOUND);
				
		ClientUserVo userVo = BaseVoUtil.getVo(tbUser, new ClientUserVo());
		this.setClientUserRelation(tbUser, userVo, tbUserLogin);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("user", userVo),HttpStatus.OK);
	}

	
	/** 
     * 异常页面控制 
     *  
     * @param runtimeException 
     * @return 
     */  
    @ExceptionHandler(RuntimeException.class)  
    @ResponseBody
    public  ResponseEntity<Map<String,Object>> runtimeExceptionHandler(RuntimeException runtimeException) {  
    	runtimeException.printStackTrace();
    	return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, runtimeException.getMessage()),HttpStatus.BAD_REQUEST);
         
    }  
}
